// -*- C++ -*-
/*!
 * @file  SoundDetection4RoIS.cpp
 * @brief Sound Detection Component for RoIS Framework
 * @date $Date$
 *
 * $Id$
 */

#include "SoundDetection4RoIS.h"

// Module specification
// <rtc-template block="module_spec">
static const char* sounddetection4rois_spec[] =
  {
    "implementation_id", "SoundDetection4RoIS",
    "type_name",         "SoundDetection4RoIS",
    "description",       "Sound Detection Component for RoIS Framework",
    "version",           "1.0.0",
    "vendor",            "ATR",
    "category",          "RoIS sample",
    "activity_type",     "PERIODIC",
    "kind",              "DataFlowComponent",
    "max_instance",      "0",
    "language",          "C++",
    "lang_type",         "compile",
    ""
  };
// </rtc-template>

/*!
 * @brief constructor
 * @param manager Maneger Object
 */
SoundDetection4RoIS::SoundDetection4RoIS(RTC::Manager* manager)
    // <rtc-template block="initializer">
  : RTC::DataFlowComponentBase(manager)

    // </rtc-template>
{
}

/*!
 * @brief destructor
 */
SoundDetection4RoIS::~SoundDetection4RoIS()
{
}



RTC::ReturnCode_t SoundDetection4RoIS::onInitialize()
{
  // Registration: InPort/OutPort/Service
  // <rtc-template block="registration">
  // Set InPort buffers
  
  // Set OutPort buffer
  
  // Set service provider to Ports
  
  // Set service consumers to Ports
  
  // Set CORBA Service Ports
  
  // </rtc-template>

	m_pComponent = NULL;
	m_pCoreCallback = NULL;
	m_core = NULL;

	return RTC::RTC_OK;
}


RTC::ReturnCode_t SoundDetection4RoIS::onFinalize()
{
	//--------------------------------------------------------
	// delete core class
	//--------------------------------------------------------
	if(m_core!=NULL)
	{
		delete m_core;
		m_core = NULL;
	}

	//--------------------------------------------------------
	// delete core callback class
	//--------------------------------------------------------
	if(m_pCoreCallback!=NULL)
	{
		delete m_pCoreCallback;
		m_pCoreCallback = NULL;
	}

	//--------------------------------------------------------
	// delete entity class
	//--------------------------------------------------------
	if(m_pComponent!=NULL)
	{
		delete m_pComponent;
		m_pComponent = NULL;
	}

	return RTC::RTC_OK;
}

/*
RTC::ReturnCode_t SoundDetection4RoIS::onStartup(RTC::UniqueId ec_id)
{
  return RTC::RTC_OK;
}
*/

/*
RTC::ReturnCode_t SoundDetection4RoIS::onShutdown(RTC::UniqueId ec_id)
{
  return RTC::RTC_OK;
}
*/


RTC::ReturnCode_t SoundDetection4RoIS::onActivated(RTC::UniqueId ec_id)
{
	//--------------------------------------------------------
	// initialize entity class
	//--------------------------------------------------------
	m_pComponent = new RoIS_Component_Entity(this);
	m_pComponent->initialize();

	//--------------------------------------------------------
	// initialize core class
	//--------------------------------------------------------
	start_core();

	//--------------------------------------------------------
	// connect to Engine
	//--------------------------------------------------------
	connect();

	return RTC::RTC_OK;
}


RTC::ReturnCode_t SoundDetection4RoIS::onDeactivated(RTC::UniqueId ec_id)
{
	//--------------------------------------------------------
	// delete core class
	//--------------------------------------------------------
	if(m_core!=NULL)
	{
		delete m_core;
		m_core = NULL;
	}

	//--------------------------------------------------------
	// disconnect from Engine
	//--------------------------------------------------------
	disconnect();

	//--------------------------------------------------------
	// finalize entity class
	//--------------------------------------------------------
	m_pComponent->finalize();

	return RTC::RTC_OK;
}

/*
RTC::ReturnCode_t SoundDetection4RoIS::onExecute(RTC::UniqueId ec_id)
{
  return RTC::RTC_OK;
}
*/

/*
RTC::ReturnCode_t SoundDetection4RoIS::onAborting(RTC::UniqueId ec_id)
{
  return RTC::RTC_OK;
}
*/

/*
RTC::ReturnCode_t SoundDetection4RoIS::onError(RTC::UniqueId ec_id)
{
  return RTC::RTC_OK;
}
*/

/*
RTC::ReturnCode_t SoundDetection4RoIS::onReset(RTC::UniqueId ec_id)
{
  return RTC::RTC_OK;
}
*/

/*
RTC::ReturnCode_t SoundDetection4RoIS::onStateUpdate(RTC::UniqueId ec_id)
{
  return RTC::RTC_OK;
}
*/

/*
RTC::ReturnCode_t SoundDetection4RoIS::onRateChanged(RTC::UniqueId ec_id)
{
  return RTC::RTC_OK;
}
*/

///////////////////// Additional Part /////////////////////
/**
 * For System Interface
 */

/**
 * For Command Interface
 */
RoIS::ReturnCode_t
SoundDetection4RoIS::bind()
{
	showDebugMessage(">>>BIND");
	RoIS::ReturnCode_t roRes = RoIS::RoIS_ERROR;

	if(this->m_pComponent==NULL)
	{
		showDebugMessage("not initialized.");
		return roRes;
	}

	roRes = m_pComponent->bind();

	showDebugMessage(m_pComponent->getLastErrorMessage().c_str());
	return roRes;
}

RoIS::ReturnCode_t
SoundDetection4RoIS::release()
{
	showDebugMessage(">>>RELEASE");
	RoIS::ReturnCode_t roRes = RoIS::RoIS_ERROR;

	if(m_pComponent==NULL)
	{
		showDebugMessage("not initialized.");
		return roRes;
	}

	roRes = m_pComponent->release();

	showDebugMessage(m_pComponent->getLastErrorMessage().c_str());
	return roRes;
}

RoIS::ReturnCode_t
SoundDetection4RoIS::get_parameter(
		RoIS::ParameterList& parameters
	)
{
	showDebugMessage(">>>GET PARAMETER");
	RoIS::ReturnCode_t roRes = RoIS::RoIS_ERROR;

	if(m_pComponent==NULL)
	{
		showDebugMessage("not initialized.");
		return roRes;
	}

	roRes = m_pComponent->get_parameter(
		parameters
	);

	showDebugMessage(m_pComponent->getLastErrorMessage().c_str());
	return roRes;
}

RoIS::ReturnCode_t
SoundDetection4RoIS::set_parameter(
		const RoIS::ParameterList& parameters,
		const char* command_id
	)
{
	showDebugMessage(">>>SET PARAMETER");
	RoIS::ReturnCode_t roRes = RoIS::RoIS_ERROR;

	if(m_pComponent==NULL)
	{
		showDebugMessage("not initialized.");
		return roRes;
	}

	roRes = m_pComponent->set_parameter(
		parameters,
		command_id
	);

	showDebugMessage(m_pComponent->getLastErrorMessage().c_str());
	return roRes;
}

RoIS::ReturnCode_t
SoundDetection4RoIS::execute(
		const char* command_type,
		const RoIS::ArgumentList& arguments,
		const char* command_id
	)
{
	showDebugMessage(">>>EXECUTE");
	RoIS::ReturnCode_t roRes = RoIS::RoIS_ERROR;

	if(m_pComponent==NULL)
	{
		showDebugMessage("not initialized.");
		return roRes;
	}

	roRes = m_pComponent->execute(
		command_type,
		arguments,
		command_id
	);

	showDebugMessage(m_pComponent->getLastErrorMessage().c_str());
	return roRes;
}

/**
 * For Query Interface
 */
RoIS::ReturnCode_t
SoundDetection4RoIS::query(
		const char* query_type,
		const char* condition,
		RoIS::ResultList& results
	)
{
	showDebugMessage(">>>QUERY");
	RoIS::ReturnCode_t roRes = RoIS::RoIS_ERROR;

	if(m_pComponent==NULL)
	{
		showDebugMessage("not initialized.");
		return roRes;
	}

	roRes = m_pComponent->query(
		query_type,
		condition,
		results
	);

	showDebugMessage(m_pComponent->getLastErrorMessage().c_str());
	return roRes;
}

/**
 * For Event Interface
 */
RoIS::ReturnCode_t
SoundDetection4RoIS::subscribe(
		const char* event_type,
		const char* condition,
		const char* subscribe_id
	)
{
	showDebugMessage(">>>SUBSCRIBE");
	RoIS::ReturnCode_t roRes = RoIS::RoIS_ERROR;

	if(m_pComponent==NULL)
	{
		showDebugMessage("not initialized.");
		return roRes;
	}

	roRes = m_pComponent->subscribe(
		event_type,
		condition,
		subscribe_id
	);

	showDebugMessage(m_pComponent->getLastErrorMessage().c_str());
	return roRes;
}

RoIS::ReturnCode_t
SoundDetection4RoIS::unsubscribe(
		const char* subscribe_id
	)
{
	showDebugMessage(">>>UNSUBSCRIBE");
	RoIS::ReturnCode_t roRes = RoIS::RoIS_ERROR;

	if(m_pComponent==NULL)
	{
		showDebugMessage("not initialized.");
		return roRes;
	}

	roRes = m_pComponent->unsubscribe(
		subscribe_id
	);

	showDebugMessage(m_pComponent->getLastErrorMessage().c_str());
	return roRes;
}

/**
 * For Engine Interface
 */
bool
SoundDetection4RoIS::connect()
{
	showDebugMessage("-----<connect>-----");
	bool bRes = false;

	if(m_pComponent==NULL)
	{
		showDebugMessage("not initialized.");
		return bRes;
	}

	bRes = m_pComponent->connect();

	showDebugMessage(m_pComponent->getLastErrorMessage().c_str());
	return bRes;
}

bool
SoundDetection4RoIS::disconnect()
{
	showDebugMessage("-----<disconnect>-----");
	bool bRes = false;

	if(m_pComponent==NULL)
	{
		showDebugMessage("not initialized.");
		return bRes;
	}

	bRes = m_pComponent->disconnect();

	showDebugMessage(m_pComponent->getLastErrorMessage().c_str());
	return bRes;
}

void
SoundDetection4RoIS::receive_error(
		RoIS::ErrorType error_type,
		rois::ParameterList* results
	)
{
	showDebugMessage("-----<receive_error>-----");

	if(m_pComponent==NULL)
	{
		showDebugMessage("not initialized.");
		return;
	}

	m_pComponent->receive_error(
		error_type,
		results
	);

	return;
}
void
SoundDetection4RoIS::completed(
		const char* command_id,
		RoIS::Completed_Status status,
		rois::ParameterList* results
	)
{
	showDebugMessage("-----<completed>-----");

	if(m_pComponent==NULL)
	{
		showDebugMessage("not initialized.");
		return;
	}

	m_pComponent->completed(
		command_id,
		status,
		results
	);

	return;
}
void
SoundDetection4RoIS::receive_event(
		const char* event_type,
		const char* subscribe_id,
		rois::ParameterList* results
	)
{
	showDebugMessage("-----<receive_event>-----");

	if(m_pComponent==NULL)
	{
		showDebugMessage("not initialized.");
		return;
	}

	m_pComponent->receive_event(
		event_type,
		subscribe_id,
		results
	);

	return;
}

void
SoundDetection4RoIS::showDebugMessage(
		string strMessage
	)
{
	string out_string = strMessage + "\n";
//	OutputDebugString(out_string.c_str());
	printf(out_string.c_str());
}

/**
 * For Core
 */
void
SoundDetection4RoIS::start_core()
{
	showDebugMessage("-----<start core>-----");
	showDebugMessage("starting core...");

	// create core callback class
	m_pCoreCallback = new RoIS_Core_Callback(this);

	// create core class
	m_core = new SoundDetectionCore;
	m_core->set_func_receive_error_ptr((func_pointer)&m_pCoreCallback->get_func_receive_error_ptr);
	m_core->set_func_completed_ptr((func_pointer)&m_pCoreCallback->get_func_completed_ptr);
	m_core->set_func_receive_event_ptr((func_pointer)&m_pCoreCallback->get_func_receive_event_ptr);
	showDebugMessage("done.");

	return;
}

extern "C"
{
 
  void SoundDetection4RoISInit(RTC::Manager* manager)
  {
    coil::Properties profile(sounddetection4rois_spec);
    manager->registerFactory(profile,
                             RTC::Create<SoundDetection4RoIS>,
                             RTC::Delete<SoundDetection4RoIS>);
  }
  
};


